Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		Super.OnCreate()
				
		DISPLAY.SetVirtualSize(320,480)
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		Return 0
	End
	
	
End

Class TMyTouchInput Extends TTouchInput

	Field screen:TTmpScreen
	
	Method New(scr:TTmpScreen)
		Self.screen = scr
	End Method
	
	Method OnTouchHit:Void(x:Int, y:Int, pointer:Int)
		Self.screen.x[pointer] = x 
		Self.screen.y[pointer] = y 
		Self.screen.vx[pointer] = 0
		Self.screen.vy[pointer] = 0
		Self.screen.down[pointer] = True
	End
		
	Method OnTouchReleased:Void(x:Int, y:Int, pointer:Int)
		Self.screen.x[pointer] = x 
		Self.screen.y[pointer] = y 
		Self.screen.down[pointer] = False
		Self.screen.life[pointer] = Self.screen.DEFAULT_LIFE
	End
	
	
	Method OnTouchDragged:Void(x:Int, y:Int, dx:Int, dy:Int, pointer:Int)
		Self.screen.x[pointer] = x 
		Self.screen.y[pointer] = y 
	End
	
	Method OnTouchClick:Void(x:Int, y:Int, pointer:Int)
		If pointer = 1 Then screen.useBigCircles = Not screen.useBigCircles
	End
  
	
	Method OnTouchLongPress:Void(x:Int, y:Int, pointer:Int)
	' if we long press, change the background colour to something random
		screen.red = Rnd(0,255)
		screen.green = Rnd(0,255)
		screen.blue = Rnd(0,255)
		If screen.red+screen.green+screen.blue < 192 Then
			screen.red = Rnd(128,255)
			screen.green = Rnd(128,255)
			screen.blue = Rnd(128,255)
		End
	End
	
	Method OnTouchFling:Void(releaseX:Int, releaseY:Int, velocityX:Float, velocityY:Float, velocitySpeed:Float, pointer:Int)
		Self.screen.x[pointer] = releaseX
		Self.screen.y[pointer] = releaseY
		Self.screen.vx[pointer] = velocityX
		Self.screen.vy[pointer] = velocityY
	End


End Class

Class TTmpScreen Extends TScreen
	
	Field TOUCH:TTouchControl
	
	Const CIRCLE_RADIUS:Float = 20
	Const DEFAULT_LIFE:Float = 2000
	Const EPSILON:Float = 3
	Field x:Float[32]
	Field y:Float[32]
	Field vx:Float[32]
	Field vy:Float[32]
	Field life:Float[32]
	Field down:Bool[32]
	Field red:Int, green:Int, blue:Int
	Field useBigCircles:Bool
	
	' START when app is created
	Method Start:Void()
		Self.TOUCH				= New TTouchControl()	
		Self.TOUCH.input 		= New TMyTouchInput(self)
		Self.TOUCH.control.MonitorTouch(True)
	End
	
	' main app logic loop update
	Method Update:Void()
	
		Self.TOUCH.Update()
		
		' for each of the circles
		For Local i:Int = 0 Until life.Length
			' if we're not touching the screen for this circle and it has some life left
			If Not down[i] And life[i]>0 Then
				' slow down the velocity
				vx[i] *= 0.95
				vy[i] *= 0.95
				
				' move the circles
				x[i] += vx[i]*AppSpeed()/100
				y[i] += vy[i]*AppSpeed()/100
				
				' dodgy bouncing code
				If x[i]<0 Then
					x[i] *= -1
					vx[i] *= -1
				End
				If x[i]>DISPLAY.VSCREEN_WIDTH Then
					x[i] = 2*DISPLAY.VSCREEN_WIDTH - x[i]
					vx[i] *= -1
				End
				If y[i]<0 Then
					y[i] *= -1
					vy[i] *= -1
				End
				If y[i]>DISPLAY.VSCREEN_HEIGHT Then
					y[i] = 2*DISPLAY.VSCREEN_HEIGHT - y[i]
					vy[i] *= -1
				End
				If vx[i]*vx[i]+vy[i]*vy[i]<=EPSILON*EPSILON Then
					vx[i] = 0
					vy[i] = 0
				End
			
				' if it's stopped moving
				If vx[i]=0 And vy[i]=0 Then
					' decrease the life
					life[i] -= AppSpeed()
					' clip it to 0
					If life[i] < 0 Then life[i] = 0
				End
			End
		Next
	End
	
	' main app render loop
	Method Render:Void()
		' fill the background with an awful colour
		Cls(red, green, blue)
		' for each of the circles
		For Local i:Int = 0 Until life.Length
			' if we're touching the screen for this circle, or it has some life left
			If life[i]>0 Or down[i] Then
				' if we're touching the screen or its life is over half
				If down[i] Or life[i] > DEFAULT_LIFE/2 Then
					' draw at full alpha
					SetAlpha(1)
				Else
					' otherwise fade it out
					SetAlpha(life[i]/(DEFAULT_LIFE/2))
				End
				' draw a white circle and reset the alpha
				SetColor(255,255,255)
				If i=1 And useBigCircles Then
					DrawCircle(x[i],y[i],2*CIRCLE_RADIUS)
				Else
					DrawCircle(x[i],y[i],CIRCLE_RADIUS)
				End
				SetAlpha(1)
			End
		Next
		SetColor(255,255,255)
		Self.DrawInfo()
	End
	
	Method DrawInfo:Void()
		DrawText("Diddy's Touch Test", 0, 0)
		DrawText("Press to move the circle and release to 'fling' it!", 0, 15)
		DrawText("Press down for a few seconds to change the background colour", 0, 30)
		DrawText("Do two touches to switch between big and small circles just for that fling", 0, 45)
		APP.DrawFPS(0, 60)
	End
End
